<script type="text/html" template lay-done="layui.data.sendParams(d.params)">
  <div class="layui-card">
    <div class="layadmin-search">
      <form class="layui-form" method="post" lay-filter="form_early_buy_search">
        <div class="layui-inline">
          <div class="layui-form-label">手机号搜索</div>
          <div class="layui-input-inline">
            <input type="text" name="keyword" value="{{decodeURIComponent(layui.router().search.keyword || '')}}"
                   class="layui-input"
                   placeholder="请输入关键字">
          </div>
        </div>
        <div class="layui-inline layui-btn-group">
          <button type="submit" lay-submit class="layui-btn" lay-filter="submit_early_buy_search">
            <i class="layui-icon layui-icon-search"></i>查询
          </button>
          <button type="submit" lay-submit class="layui-btn layui-btn-warm"
                  lay-filter="submit_early_buy_set">
            <i class="layui-icon layui-icon-add-1"></i>提前购设置
          </button>
        </div>
      </form>
    </div>
    <div class="layui-card-body">
      <div id="table_early_buy" lay-filter="table_early_buy"></div>
    </div>
  </div>
</script>
<script>
  layui.data.sendParams = function (obj) {
    layui.use(['table'], function () {
      let $ = layui.$
        , view = layui.view
        , admin = layui.admin
        , form = layui.form
        , table = layui.table

      // 列表查询
      table.render({
        elem: '#table_early_buy'
        , url: 'agent-admin/products/' + obj.id + '/early_buy'
        , page: true
        , cols: [[
          {title: '手机号', field: 'user_mobile', minWidth: 150}
          , {title: '提前购买时间', field: 'early_buy_minutes', minWidth: 200}
          , {title: '可购买数量', field: 'buy_limit', minWidth: 150}
          , {title: '添加时间', field: 'created_at', width: 180, align: "center"}
        ]]
      });

      //搜索
      form.on('submit(submit_early_buy_search)', function (d) {
        table.reload('table_early_buy', {where: {keyword: d.field.keyword}, page: {curr: 1}});
        return false;
      });
      //添加
      form.on('submit(submit_early_buy_set)', function (d) {
        showEarlyBuyForm()
        return false
      });


      //提前购设置
      function showEarlyBuyForm() {
        let minWidth = '500px';
        let minHeight = '280px';
        admin.popup({
          title: '提前购设置'
          , area: [minWidth, minHeight]
          , id: 'popup_early_buy_form'
          , success: function (layero, index) {
            view(this.id).render('/product/early_buy_form', {product_id: obj.id}).done(function () {
              form.on('submit(submit_early_buy)', function (d) {
                if (d.field.is_uploaded == 0) {
                  layer.msg("请先上传用户清单")
                  return
                }
                admin.post('agent-admin/products/' + obj.id + '/early_buy', {
                  early_buy_minutes: d.field.early_buy_minutes
                }, function (res) {
                  layer.close(index)
                  table.reload('table_early_buy', {})
                })
              })
            })
          }
        })
      }
    })
  }
</script>
